package cn.wit.summit.chwan.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.tools.downloadtool.DownloadTool;
import cn.wit.summit.chwan.ui.base.MyActivity;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.MD5Utils;
import cn.wit.summit.chwan.utils.NetWorkUtils;
import cn.wit.summit.chwan.utils.PreferenceUtil;
import cn.wit.summit.chwan.utils.RegularUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.utils.ToastUtils;
import retrofit2.Retrofit;


/**
 * 账号密码登录
 */
public class AccountLoginActivity extends MyActivity {

    @Bind(R.id.ed_iphone)
    EditText ed_iphone;
    @Bind(R.id.et_iphone_code)
    EditText ed_iphone_code;
    @Bind(R.id.btn_login)
    Button btn_login;
    @Bind(R.id.btn_yan_login)
    Button btn_yan_login;
    boolean iflogin = true;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            showSoftInput(ed_iphone);
        }
    };

    private RetrofitManager retrofitManager;
    private Retrofit retrofitLogin;
    private ApiUtil apiLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_login);
        ButterKnife.bind(this);
        setBarTitleTx("登录");
        retrofitManager = RetrofitManager.getInstance(mContext);
        retrofitLogin = retrofitManager.getLoginRetrofit();
        apiLogin = retrofitLogin.create(ApiUtil.class);
//        showSoftInput(ed_iphone);
        handler.sendEmptyMessageDelayed(1, 500);
    }

    @Override
    protected void onPause() {
        super.onPause();
        hideSoftInput(ed_iphone);
        hideSoftInput(ed_iphone_code);
    }

    @OnClick({R.id.btn_login, R.id.btn_yan_login})
    public void onViewOnclick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                getResult();

                break;
            case R.id.btn_yan_login:
                //跳转验证码登录
                startActivity(new Intent(AccountLoginActivity.this, MainLoginActivity.class));
                finish();
                break;

        }
    }

    /**
     * 账号登录
     */
    private void getResult() {
        hideSoftInput(ed_iphone);
        hideSoftInput(ed_iphone_code);
        String account = ed_iphone.getText().toString().trim();
        String pwd = ed_iphone_code.getText().toString().trim();
        if (account == null || account.equals("")) {
            ToastUtils.getInstance(mContext).showToastSystem("账号不能为空");

            return;
        }
        if (pwd == null || pwd.equals("")) {
            ToastUtils.getInstance(mContext).showToastSystem("密码不能为空");

            return;
        }
        if (pwd.length() < 6 || pwd.length() > 20) {
            ToastUtils.getInstance(mContext).showToastSystem("密码长度低于6位或高于20位");
            return;
        }

//        if (RegularUtil.getPwd(pwd) == false) {
//            ToastUtils.getInstance(mContext).showToastSystem("密码含有其他字符");
//            return;
//        }

        String newPwd = MD5Utils.encode(pwd);
        if (StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(newPwd)) {
            if (NetWorkUtils.isNetworkConnected(AccountLoginActivity.this)) {
                if (RegularUtil.getIphoneCorrect(account) == true) {
                    apiLogin.getAccountLogin(account, newPwd).enqueue(new MyCallback<ResponseBase<AccountBean>>() {
                        @Override
                        public void onSuccess(ResponseBase<AccountBean> model) {
                            AccountBean data = model.getData_info();
                            LogUtils.e(data.getToken());
                            setSP(data);
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            ToastUtils.getInstance(mContext).showToastSystem(msg);
                        }
                    });
                } else {
                    ToastUtils.getInstance(mContext).showToastSystem("手机号码格式不正确");
                }

            } else {

                ToastUtils.getInstance(mContext).showToastSystem("网络连接失败，再试试吧~");
            }
        } else {
            Toast.makeText(mContext, "账号或密码为空", Toast.LENGTH_SHORT).show();
        }
    }

    private void setSP(AccountBean data) {

        AccountUtil.getInstance().saveAccountData(data);
        hideSoftInput(ed_iphone);
        hideSoftInput(ed_iphone_code);

        if (data.getDefaultNickname()==0 || data.getAvatar().contains("avatar_default")) {//未设置昵称
            Intent intent = new Intent(mContext, SetPersonalInformationActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("login_data", data);
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
            return;
        }
        ToastUtils.getInstance(mContext).showToastSystem("登录成功");
        String gameid=PreferenceUtil.getPreferences(this,"toursEnergyPreGameID");
        if (StringUtils.isNotEmpty(gameid)){
            DownloadTool.sendMessageEnergy(this,gameid);
        }
        finish();
    }


}
